import { TypeOrmModuleOptions } from "@nestjs/typeorm";
import { environmentConfig } from "./environment.config";

export const databaseConfig: TypeOrmModuleOptions = {
  type: "mysql",
  host: process.env.DB_HOST || environmentConfig.database.host,
  port: parseInt(process.env.DB_PORT || environmentConfig.database.port.toString(), 10),
  username: process.env.DB_USER || environmentConfig.database.username,
  password: process.env.DB_PASS || environmentConfig.database.password,
  database: process.env.DB_NAME || environmentConfig.database.database,
  autoLoadEntities: true,
  synchronize: false, // 暂时禁用自动同步，避免MySQL 5.5时间戳语法错误
  timezone: "+08:00",
  logging: false,
  dropSchema: false,
  migrationsRun: false,
  dateStrings: true,
  // 禁用时间戳精度 - 移除不支持的属性
  extra: {
    charset: "utf8mb4",
    timezone: "+08:00",
    multipleStatements: true,
    dateStrings: true,
    supportBigNumbers: true,
    bigNumberStrings: true,
    typeCast: true,
    // MySQL 5.5兼容性设置
    version: "5.5",
    // 禁用时间戳精度
    decimalNumbers: true,
    // 设置MySQL模式
    flags: ["-FOUND_ROWS"],
    // 禁用时间戳精度
    acquireTimeout: 60000,
    timeout: 60000,
  },
};
